package com.gjbao.httpcument;

import lombok.Builder;
import lombok.Data;
import org.apache.commons.codec.digest.DigestUtils;
import org.junit.Test;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;


import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class HttpsRest {

    //用户帐号id
    private static final String owner_id = "1000015399";  //1100016255  1000015399
    //API授权接入方唯一身份标识
    private static final String api_id = "9b53f0eee5bc4d3792c9603986bcf85c";//103
    //开通API授权后获得的私钥
    private static final String api_key = "3c519b51fb7e48fbac23fbd69c9aa9e3"; //e5c7e38c5e99406684f967bd8fca652d


    @Test
    public void test1() throws UnsupportedEncodingException {



        RestTemplate restTemplate = new RestTemplate();

//        String Url = "https://sapi.ads.oppomobile.com/v2/feeds/template/config";

        List<HttpMessageConverter<?>> messageConverters = restTemplate.getMessageConverters();

        messageConverters.remove(1);

        // 设置字符编码为utf-8
        HttpMessageConverter<?> converter = new StringHttpMessageConverter(StandardCharsets.UTF_8);
        messageConverters.add(1, converter); // 添加新的转换器(注:convert顺序错误会导致失败)
        restTemplate.setMessageConverters(messageConverters);


        //------请求头设置
        HttpHeaders httpHeaders = new HttpHeaders ();
//        httpHeaders.add("Content-type","application/x-www-form-urlencoded; charset=utf-8");
        httpHeaders.add("Content-type","application/json; charset=utf-8");
        httpHeaders.add("Authorization", "Bearer "+generateToken());


        // -------------------------------> URI
        StringBuffer paramsURL = new StringBuffer("https://sapi.ads.oppomobile.com/v2/feeds/template/config");
        // 字符数据最好encoding一下;这样一来，某些特殊字符才能传过去(如:flag的参数值就是“&”,不encoding的话,传不过去)
//        paramsURL.append("?flag=" + URLEncoder.encode("&", "utf-8"));
        paramsURL.append("?ownerId=1000016570");

        URI uri = URI.create(paramsURL.toString());

        // -------------------------------> 注:GET请求 创建HttpEntity时,请求体传入null即可
        // 请求体的类型任选即可;只要保证 请求体 的类型与HttpEntity类的泛型保持一致即可
        String httpBody = null;
        HttpEntity<String> httpEntity = new HttpEntity<String>(httpBody, httpHeaders);


        //  -------------------------------> 执行请求并返回结果
        // 此处的泛型  对应 响应体数据   类型;即:这里指定响应体的数据装配为String
        ResponseEntity<String> response =
                restTemplate.exchange(uri, HttpMethod.GET, httpEntity, String.class);

        // -------------------------------> 响应信息

        int statusCodeValue = response.getStatusCodeValue();
        System.out.println("响应码:"+statusCodeValue);

        System.out.println("响应tou::"+response.getHeaders());
        System.out.println("相应ti:"+response.getBody());

    }


    public  String generateToken(){
        String time_stamp = String.valueOf(System.currentTimeMillis()/1000);
        String token = null;
        String base64 = owner_id+","+api_id+","+time_stamp+","+generateSign(api_id,api_key,time_stamp);
        token =Base64.encode(base64.getBytes());
        return token;
    }

    public  String generateSign(String api_id,String api_key,String time_stamp){
        String str =api_id+api_key+time_stamp;
        String sign = DigestUtils.shaHex(str);
        return sign;
    }


    @Test
    public void postTest() throws UnsupportedEncodingException {

//        RestTemplate restTemplate = new RestTemplate(new HttpsClientRequestFactory());
        RestTemplate restTemplate = new RestTemplate();

        List<HttpMessageConverter<?>> messageConverters = restTemplate.getMessageConverters();

        messageConverters.remove(1);

        // 设置字符编码为utf-8
        HttpMessageConverter<?> converter = new StringHttpMessageConverter(StandardCharsets.UTF_8);
        messageConverters.add(1, converter); // 添加新的转换器(注:convert顺序错误会导致失败)
        restTemplate.setMessageConverters(messageConverters);


        //------请求头设置
        HttpHeaders httpHeaders = new HttpHeaders ();
        httpHeaders.add("Content-Type","application/x-www-form-urlencoded; charset=utf-8");
//        httpHeaders.add("Content-type","application/json; charset=utf-8");
        httpHeaders.add("Authorization", "Bearer "+generateToken());


        String httpBody = "ownerId=1000016570, adId=101166523";
        HttpEntity<String> httpEntity = new HttpEntity<String>(httpBody, httpHeaders);


//        MultiValueMap<String, Long> map= new LinkedMultiValueMap<String, Long>();
//        map.add("ownerId", 1000016570l);
//        map.add("adId", 101166523l);
//        HttpEntity<MultiValueMap<String, Long>> httpEntity = new HttpEntity<MultiValueMap<String, Long>>(map, httpHeaders);


        //以map形式传参，后端可接收 @RequestBody Map<String,Long> results
//        Map<String ,Long> map = new HashMap<>();
//        map.put("ownerId", 1000016570l);
//        map.put("adId", 101166523l);
//        HttpEntity<Map<String ,Long>> httpEntity = new HttpEntity<Map<String ,Long>>(map, httpHeaders);

        // -------------------------------> URI

        StringBuffer paramsURL = new StringBuffer("http://localhost:8080/posthttp");

        URI uri = URI.create(paramsURL.toString());


        ResponseEntity<String> response =
                restTemplate.exchange(uri, HttpMethod.POST, httpEntity, String.class);

        System.err.println(response.getStatusCodeValue());

        System.out.println(response.getHeaders());

        System.out.println(response.getBody());


    }




//------勿动

    @Test
    public void postTest2() throws UnsupportedEncodingException {

//        RestTemplate restTemplate = new RestTemplate(new HttpsClientRequestFactory());
        RestTemplate restTemplate = new RestTemplate(new HttpsClientRequestFactory());

        List<HttpMessageConverter<?>> messageConverters = restTemplate.getMessageConverters();

        messageConverters.remove(1);

        // 设置字符编码为utf-8
        HttpMessageConverter<?> converter = new StringHttpMessageConverter(StandardCharsets.UTF_8);
        messageConverters.add(1, converter); // 添加新的转换器(注:convert顺序错误会导致失败)
        restTemplate.setMessageConverters(messageConverters);


        //------请求头设置
        HttpHeaders httpHeaders = new HttpHeaders ();
        httpHeaders.add("Content-Type","application/x-www-form-urlencoded; charset=utf-8");
//        httpHeaders.add("Content-type","application/json; charset=utf-8");
        httpHeaders.add("Authorization", "Bearer "+generateToken());


//        String httpBody = "ownerId=1000016570, adId=101166523";
//        HttpEntity<String> httpEntity = new HttpEntity<String>(httpBody, httpHeaders);

        MultiValueMap<String, Long> map= new LinkedMultiValueMap<String, Long>();
        map.add("ownerId", 1000016570l);
        map.add("adId", 101166523l);

        HttpEntity<MultiValueMap<String, Long>> httpEntity = new HttpEntity<MultiValueMap<String, Long>>(map, httpHeaders);


        // -------------------------------> URI
        StringBuffer paramsURL = new StringBuffer("https://sapi.ads.oppomobile.com/v2/union/advert/ad/Q/detail");


        URI uri = URI.create(paramsURL.toString());


        ResponseEntity<String> response =
                restTemplate.exchange(uri, HttpMethod.POST, httpEntity, String.class);

        System.err.println(response.getStatusCodeValue());

        System.out.println(response.getHeaders());

        System.out.println(response.getBody());


    }
}
